<!doctype html>
<html class="theme-next use-motion theme-next-mist">
<head>
  
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

  <meta name="google-site-verification" content="K8DCBviaoTBKVs28YBB7IBIbospQ9RVlgSh81RYMUhY" />


  <meta name="baidu-site-verification" content="tXr3ZTm3Hx" />



  <link rel="stylesheet" type="text/css" href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5"/>


<link rel="stylesheet" type="text/css" href="/css/main.css?v=0.4.5.1"/>

  <meta name="description" content="xgfe's blog. 鲜果前端的技术博客，鲜果前端研发部官方博客。前端基础技术研究：html, html5, javascript, css, css3；前端框架研究：angularJs, react, react native." />


  <meta name="keywords" content="Vue.js," />


  <link rel="alternate" target="_blank" href="/atom.xml" title="xgfe" type="application/atom+xml" />


  <link rel="shorticon icon" type="image/x-icon" href="http://p0.meituan.net/xgfe/2db359f56ce13be30dedef160e0e57ce16958.ico?v=0.4.5.1" />

<meta name="description" content="Vue作为一个前端轻量级的MVVM框架有其独到之处,本文主要针对Vue1.0版本的官方文档进行梳理总结,主要包括以下几个方面:1.数据绑定2.指令3.组件4.事件5.过滤器">
<meta name="keywords" content="Vue.js">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue实战(一) -- Vue基础总结">
<meta property="og:url" content="http://xgfe.github.io/2016/11/09/zhouxiong/vue-in-action-vue-basis/index.html">
<meta property="og:site_name" content="xgfe">
<meta property="og:description" content="Vue作为一个前端轻量级的MVVM框架有其独到之处,本文主要针对Vue1.0版本的官方文档进行梳理总结,主要包括以下几个方面:1.数据绑定2.指令3.组件4.事件5.过滤器">
<meta property="og:image" content="http://p0.meituan.net/dpnewvc/43cca72c865daf323a732de2c8924743116787.png">
<meta property="og:updated_time" content="2017-04-12T08:03:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue实战(一) -- Vue基础总结">
<meta name="twitter:description" content="Vue作为一个前端轻量级的MVVM框架有其独到之处,本文主要针对Vue1.0版本的官方文档进行梳理总结,主要包括以下几个方面:1.数据绑定2.指令3.组件4.事件5.过滤器">
<meta name="twitter:image" content="http://p0.meituan.net/dpnewvc/43cca72c865daf323a732de2c8924743116787.png">


<script type="text/javascript" id="hexo.configuration">
  var CONFIG = {
    scheme: 'Mist',
    sidebar: 'post'
  };
</script>

  <title> Vue实战(一) -- Vue基础总结 | xgfe </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  <div style="position: fixed; top: -9999px; left: -9999px;">
    <img src="http://p0.meituan.net/xgfe/082a9624ba5ae8602150a2d43968463e49348.png" alt="xgfe"/>
  </div>
  <!--[if lte IE 8]>
  <div style=' clear: both; height: 59px; padding:0 0 0 15px; position: relative;margin:0 auto;'>
    <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode">
      <img src="http://7u2nvr.com1.z0.glb.clouddn.com/picouterie.jpg" border="0" height="42" width="820"
           alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today or use other browser ,like chrome firefox safari."
           style='margin-left:auto;margin-right:auto;display: block;'/>
    </a>
  </div>
<![endif]-->
  

  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?3601d4483819a5ab6ddabb0b6422a328";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <div class="container one-column page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><h1 class="site-meta">
  <span class="logo-line-before"><i></i></span>
  <a href="/" class="brand" rel="start">
      <span class="logo">
        <i class="icon-next-logo"></i>
      </span>
      <span class="site-title">xgfe</span>
  </a>
  <span class="logo-line-after"><i></i></span>
</h1>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  
  

  
    <ul id="menu" class="menu menu-left">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            <i class="menu-item-icon icon-next-home"></i> <br />
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            <i class="menu-item-icon icon-next-archives"></i> <br />
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            <i class="menu-item-icon icon-next-tags"></i> <br />
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-join">
          <a href="/join" rel="section">
            <i class="menu-item-icon icon-next-join"></i> <br />
            加入我们
          </a>
        </li>
      
      <!-- slide-links added by felix -->
      <li class="menu-item menu-item-slides" style="opacity: 1; transform: translateY(0px);">
        <a href="http://xgfe.github.io/Basics/" target="_blank" rel="section">
          <i class="menu-item-icon icon-next-slides"></i> <br>
          Basics
        </a>
      </li>
      <li class="menu-item menu-item-slides" style="opacity: 1; transform: translateY(0px);">
        <a href="https://slides.com/xgfe" target="_blank" rel="section">
          <i class="menu-item-icon icon-next-slides"></i> <br>
          Slides
        </a>
      </li>

      
      
    </ul>
  

  
    <div class="site-search">
      

    </div>
  

    <div class="site-search">
      <form class="site-search-form" id="gg-form" action="https://www.google.com/webhp" >
        <input type="text" name="q" id="gg-search-input" class="menu-search-input">
      </form>
    </div>
</nav>
 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div id="content" class="content"> 

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">

      
      
        <h1 class="post-title" itemprop="name headline">
          
          
            
              Vue实战(一) -- Vue基础总结
            
          
        </h1>
      

      <div class="post-meta">
        <span class="post-time">
          发表于
          <time itemprop="dateCreated" datetime="2016-11-09T12:00:00+08:00" content="2016-11-09">
            2016-11-09
          </time>
        </span>

        
          <span class="post-category" >
            &nbsp; | &nbsp; 作者
            
              <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                <a href="/categories/zhouxiong/" itemprop="url" rel="index">
                  <span itemprop="name">zhouxiong</span>
                </a>
              </span>

              
              

            
          </span>
        

        
          
        

        <!-- tags 挪动位置 -->
        
          <span class="post-tags">
            &nbsp; | &nbsp;
            
              <a href="/tags/Vue-js/" rel="tag"><i class="icon-next-tags"></i>Vue.js</a>
            
          </span>
        
      </div>
    </header>

    <div class="post-body">

      
      

      
        <span itemprop="articleBody"><p>Vue作为一个前端轻量级的MVVM框架有其独到之处,本文主要针对Vue1.0版本的官方文档进行梳理总结,主要包括以下几个方面:<br>1.数据绑定<br>2.指令<br>3.组件<br>4.事件<br>5.过滤器</p>
<a id="more"></a>
<h2 id="Vue基础"><a href="#Vue基础" class="headerlink" title="Vue基础"></a>Vue基础</h2><h3 id="Vue实例"><a href="#Vue实例" class="headerlink" title="Vue实例"></a>Vue实例</h3><h4 id="构造器"><a href="#构造器" class="headerlink" title="构造器"></a>构造器</h4><p>通过构造函数可以创建一个Vue的根实例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">var vm = new Vue(&#123;</div><div class="line">  // 选项</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>一个Vue的实例是MVVM模型中的ViewModel，在实例化时，需要传入一个选项对象，其中包括挂载元素、数据、模板、方法，生命周期的钩子函数等</p>
<h4 id="实例生命周期"><a href="#实例生命周期" class="headerlink" title="实例生命周期"></a>实例生命周期</h4><p><img src="http://p0.meituan.net/dpnewvc/43cca72c865daf323a732de2c8924743116787.png" alt="实例生命周期"></p>
<h3 id="数据绑定"><a href="#数据绑定" class="headerlink" title="数据绑定"></a>数据绑定</h3><p>Vue.js的模板是基于DOM实现的，Vue的模板都是可解析的有效HTML，且通过一些特性做了增强。</p>
<h4 id="插值"><a href="#插值" class="headerlink" title="插值"></a>插值</h4><ul>
<li>文本</li>
</ul>
<p>最基本的数据绑定形式就是文本插值，使用Mustache语法-双大括号：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;span&gt;Message: &#123;&#123; msg &#125;&#125;&lt;/span&gt;</div></pre></td></tr></table></figure>
<p>Mustache标签会被数据对象中的msg值替换，每当属性值发生变化对应也会更新到DOM上</p>
<ul>
<li>原始的HTML</li>
</ul>
<p>有的时候要输出真正的HTML字符串，则需要用到三Mustache标签，此时的数据绑定会被忽略</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div&gt;&#123;&#123;&#123; raw_html &#125;&#125;&#125;&lt;/div&gt;</div></pre></td></tr></table></figure>
<p><strong>注意：在网站上动态渲染任意的HTML是非常危险的，容易导致XSS攻击。因此只对可信的HTML使用插值，永不用于用户提交的内容</strong></p>
<ul>
<li>HTML特性</li>
</ul>
<p>Mustache标签也可以用在HTML属性中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div id=&quot;item-&#123;&#123; id &#125;&#125;&quot;&gt;&lt;/div&gt;</div></pre></td></tr></table></figure>
<p>但是在Vue.js指令和特殊特性内不能使用插值</p>
<h4 id="绑定表达式"><a href="#绑定表达式" class="headerlink" title="绑定表达式"></a>绑定表达式</h4><p>放在Mustache标签内的文本成为绑定表达式，可以由一个简单的Javascript表达式和可选的一个或多个过滤器组成</p>
<ul>
<li><p>javascript表达式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&#123;&#123; number + 1 &#125;&#125;</div><div class="line"></div><div class="line">&#123;&#123; ok ? &apos;YES&apos; : &apos;NO&apos; &#125;&#125;</div><div class="line"></div><div class="line">&#123;&#123; message.split(&apos;&apos;).reverse().join(&apos;&apos;) &#125;&#125;</div></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="过滤器"><a href="#过滤器" class="headerlink" title="过滤器"></a>过滤器</h4><p>Vue.js允许在表达式后添加可选的过滤器，以‘管道符’形式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;&#123; message | capitalize &#125;&#125;</div></pre></td></tr></table></figure>
<p>上述语句表示将<code>message</code>的值通过内置的<code>capitalize</code>过滤器，这个过滤器其实是一个函数，返回大写的值</p>
<p>过滤器可以串联</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;&#123; message | filterA | filterB &#125;&#125;</div></pre></td></tr></table></figure>
<p>同时可以接收参数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;&#123; message | filterA &apos;arg1&apos; arg2 &#125;&#125;</div></pre></td></tr></table></figure>
<p>过滤器函数始终以表达式的值作为第一个参数。带引号的参数视为字符串，而不带引号的参数按表达式计算。这里，字符串 ‘arg1’ 将传给过滤器作为第二个参数，表达式 arg2 的值在计算出来之后作为第三个参数。</p>
<h3 id="指令"><a href="#指令" class="headerlink" title="指令"></a>指令</h3><p>指令是特殊的带有前缀<code>v-</code>特性。指令的值限定为<strong>绑定表达式</strong>，它的职责是当其表达式的值改变时把某些行为应用到DOM上。</p>
<ul>
<li><p>v-if、v-show与v-else</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;p v-if=&quot;greeting&quot;&gt;Hello!&lt;/p&gt;</div></pre></td></tr></table></figure>
<p>这里通过<code>v-if</code>指令将表达式<code>greeting</code>的值真假插入/删除p元素。</p>
</li>
</ul>
<p> 上述指令用于条件渲染，根据绑定的表达式结果确定是否要渲染HTML元素。<code>v-else</code> 元素必须立即跟在 <code>v-if</code> 或 <code>v-show</code> 元素的后面——否则它不能被识别。</p>
<ul>
<li><p>区别</p>
<p><code>v-if</code> 是真实的条件渲染，因为它会确保条件块在切换当中合适地销毁与重建条件块内的事件监听器和子组件</p>
<p><code>v-show</code> 绑定的元素始终被编译并保留，只是简单地基于 CSS 切换，另外它不支持<code>&lt;template&gt;</code>标签</p>
<p>一般来说，<code>v-if</code> 有更高的切换消耗而 <code>v-show</code>有更高的初始渲染消耗。因此，如果需要频繁切换 <code>v-show</code> 较好，如果在运行时条件不大可能改变 <code>v-if</code> 较好</p>
</li>
</ul>
<ul>
<li><p>v-on</p>
<p>利用<code>v-on</code>指令用于监听DOM事件，例如：<code>&lt;a v-on:click=&quot;doSomething&quot;&gt;</code></p>
<p>另外<code>v-on</code>指令还可以缩写成<code>@</code>符号表示：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;!-- 完整语法 --&gt;</div><div class="line">&lt;a v-on:click=&quot;doSomething&quot;&gt;&lt;/a&gt;</div><div class="line"></div><div class="line">&lt;!-- 缩写 --&gt;</div><div class="line">&lt;a @click=&quot;doSomething&quot;&gt;&lt;/a&gt;</div></pre></td></tr></table></figure>
</li>
<li><p>v-bind</p>
<p>利用<code>v-bind</code>指令用于响应的更新HTML属性，例如：<code>&lt;a v-bind:href=&quot;url&quot;&gt;&lt;/a&gt;</code></p>
<p>另外<code>v-bind</code>指令还可以缩写为<code>:</code>表示：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;!-- 完整语法 --&gt;</div><div class="line">&lt;a v-bind:href=&quot;url&quot;&gt;&lt;/a&gt;</div><div class="line"></div><div class="line">&lt;!-- 缩写 --&gt;</div><div class="line">&lt;a :href=&quot;url&quot;&gt;&lt;/a&gt;</div><div class="line"></div><div class="line">&lt;!-- 完整语法 --&gt;</div><div class="line">&lt;button v-bind:disabled=&quot;someDynamicCondition&quot;&gt;Button&lt;/button&gt;</div><div class="line"></div><div class="line">&lt;!-- 缩写 --&gt;</div><div class="line">&lt;button :disabled=&quot;someDynamicCondition&quot;&gt;Button&lt;/button&gt;</div></pre></td></tr></table></figure>
</li>
<li><p>v-model</p>
<p>此指令用于表单元素上，例如常见的input，select，textarea，radio，checkbox等</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"> &lt;select v-model=&quot;selected&quot; multiple&gt;</div><div class="line"> &lt;option selected&gt;A&lt;/option&gt;</div><div class="line"> &lt;option&gt;B&lt;/option&gt;</div><div class="line"> &lt;option&gt;C&lt;/option&gt;</div><div class="line">&lt;/select&gt;</div><div class="line">&lt;br&gt;</div><div class="line">&lt;span&gt;Selected: &#123;&#123; selected | json &#125;&#125;&lt;/span&gt;</div></pre></td></tr></table></figure>
</li>
<li><p>v-for</p>
<p><code>v-for</code>指令基于一个数组渲染一个列表。还有一个特殊的变量<code>$index</code>，它是当前数组元素的索引</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"> &lt;ul id=&quot;example-1&quot;&gt;</div><div class="line">  &lt;li v-for=&quot;item in items&quot;&gt;</div><div class="line">     &#123;&#123; parentMessage &#125;&#125; - &#123;&#123; $index &#125;&#125; - &#123;&#123; item.message &#125;&#125;</div><div class="line">  &lt;/li&gt;</div><div class="line">&lt;/ul&gt;</div><div class="line"></div><div class="line">var example1 = new Vue(&#123;</div><div class="line">  el: &apos;#example-1&apos;,</div><div class="line">  data: &#123;</div><div class="line">    items: [</div><div class="line">      &#123; message: &apos;Foo&apos; &#125;,</div><div class="line">      &#123; message: &apos;Bar&apos; &#125;</div><div class="line">    ]</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>Vue.js封装了数组的变异方法，例如<code>push</code>,<code>pop</code>,<code>shift</code>, <code>unshift</code>等和非变异方法，例如<code>filter</code>,<code>concat</code>等，如果是非变异的方法，则只需要直接使用返回的新数组替换原来的数组即可</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">example1.items = example1.items.filter(function (item) &#123;</div><div class="line">     return item.message.match(/Foo/)</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<ul>
<li>track-by</li>
</ul>
<p>列表在渲染的过程中通过唯一的键进行追踪，可以通过数组中每个值的唯一id进行标识或者通过<code>$index</code>，这种模式下可以处理数组中重复的值</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"> &lt;div v-for=&quot;item in items&quot; track-by=&quot;$index&quot;&gt;</div><div class="line">&lt;!-- content --&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="计算属性"><a href="#计算属性" class="headerlink" title="计算属性"></a>计算属性</h3><p>在Vue.js中将绑定表达式限定为一个表达式，如果需要多余一个表达式的逻辑，应当使用计算属性。一个比较常见的应用方式是b属性的值依赖于a属性的值，则b应当设置为计算属性。计算属性同样具有getter和setter方法</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">var vm = new Vue(&#123;</div><div class="line">  data: &#123;</div><div class="line">    firstName: &apos;Foo&apos;,</div><div class="line">    lastName: &apos;Bar&apos;</div><div class="line">  &#125;,</div><div class="line">  computed: &#123;</div><div class="line">      fullName: &#123;</div><div class="line">	    // getter</div><div class="line">	    get: function () &#123;</div><div class="line">	     	return this.firstName + &apos; &apos; + this.lastName</div><div class="line">	    &#125;,</div><div class="line">	    // setter</div><div class="line">	    set: function (newValue) &#123;</div><div class="line">	        var names = newValue.split(&apos; &apos;)</div><div class="line">	        this.firstName = names[0]</div><div class="line">	        this.lastName = names[names.length - 1]</div><div class="line">	    &#125;</div><div class="line">     &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>此时的<code>vm. fullName</code>依赖于<code>vm. firstName</code>和<code>vm. lastName</code>，所以当<code>vm. firstName</code>和<code>vm. lastName</code>的值改变的时候，<code>vm. fullName</code>值也会相应的进行改变。同样的如果调用<code>vm.fullName = &#39;John Doe&#39;</code> 时，setter 会被调用，<code>vm.firstName</code> 和 <code>vm.lastName</code> 也会有相应更新</p>
<h3 id="Class与style绑定"><a href="#Class与style绑定" class="headerlink" title="Class与style绑定"></a>Class与style绑定</h3><p>数据绑定的一个常见需求是操作元素的class和它的内联样式。因为它们都是HTML元素的属性，可以用<code>v-bind</code>来处理它们：只需要计算出表达式最终字符串。在<code>v-bind</code>用于绑定class和style时，Vue.js专门对其进行增强，使得表达式的结果类型除了字符串外，还可以是对象或者数组。</p>
<p><strong>注意：尽管可以用Mustache标签绑定class，例如class=，但是不推荐和v-bind:class=className这种方式混用。两者只能选其一</strong></p>
<h4 id="绑定class"><a href="#绑定class" class="headerlink" title="绑定class"></a>绑定class</h4><ul>
<li>对象语法</li>
</ul>
<p>可以给<code>v-bind:class</code>传递一个对象，以动态的切换class。注意<code>v-bind:class</code>可以和普通的<code>class</code>共存。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div class=&quot;static&quot; v-bind:class=&quot;&#123; &apos;class-a&apos;: isA, &apos;class-b&apos;: isB &#125;&quot;&gt;&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">data: &#123;</div><div class="line">  isA: true,</div><div class="line">  isB: false</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>渲染为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div class=&quot;static class-a&quot;&gt;&lt;/div&gt;</div></pre></td></tr></table></figure>
<ul>
<li>数组语法</li>
</ul>
<p>可以把一个数组传递给<code>v-bind:class</code>，以应用一个class列表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div v-bind:class=&quot;[classA, classB]&quot;&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"> data: &#123;</div><div class="line">  classA: &apos;class-a&apos;,</div><div class="line">  classB: &apos;class-b&apos;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>渲染为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div class=&quot;class-a class-b&quot;&gt;&lt;/div&gt;</div></pre></td></tr></table></figure>
<h4 id="绑定style内联样式"><a href="#绑定style内联样式" class="headerlink" title="绑定style内联样式"></a>绑定style内联样式</h4><p><code>v-bind:style</code>与<code>v-bind:class</code>使用方式相同，可以使用对象语法和数组语法。css属性名可以采用驼峰式或者短横线分割命名。</p>
<ul>
<li><p>对象语法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div v-bind:style=&quot;styleObject&quot;&gt;&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">  data: &#123;</div><div class="line">	  styleObject: &#123;</div><div class="line">	      color: &apos;red&apos;,</div><div class="line">	      fontSize: &apos;13px&apos;</div><div class="line">	  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
<li><p>数组语法</p>
<p>可以将多个样式对象应用到一个元素上<br><code>&lt;div v-bind:style=&quot;[AStyle, BStyle]&quot;&gt;</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">AStyle: &#123;</div><div class="line">      color: &apos;red&apos;</div><div class="line">  &#125;,</div><div class="line">  BStyle: &#123;</div><div class="line">      fontSize: &apos;22px&apos;</div><div class="line">  &#125;</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="表单控件绑定"><a href="#表单控件绑定" class="headerlink" title="表单控件绑定"></a>表单控件绑定</h3><p>可以通过<code>v-model</code>指令在表单元素上创建双向数据绑定</p>
<h4 id="Text"><a href="#Text" class="headerlink" title="Text"></a>Text</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;span&gt;Message is: &#123;&#123; message &#125;&#125;&lt;/span&gt;</div><div class="line">&lt;br&gt;</div><div class="line">&lt;input type=&quot;text&quot; v-model=&quot;message&quot; placeholder=&quot;edit me&quot;&gt;</div></pre></td></tr></table></figure>
<h4 id="Checkbox"><a href="#Checkbox" class="headerlink" title="Checkbox"></a>Checkbox</h4><p>多个复选框，绑定到同一个数组</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;input type=&quot;checkbox&quot; id=&quot;jack&quot; value=&quot;Jack&quot; v-model=&quot;checkedNames&quot;&gt;</div><div class="line">&lt;label for=&quot;jack&quot;&gt;Jack&lt;/label&gt;</div><div class="line">&lt;input type=&quot;checkbox&quot; id=&quot;john&quot; value=&quot;John&quot; v-model=&quot;checkedNames&quot;&gt;</div><div class="line">&lt;label for=&quot;john&quot;&gt;John&lt;/label&gt;</div><div class="line">&lt;input type=&quot;checkbox&quot; id=&quot;mike&quot; value=&quot;Mike&quot; v-model=&quot;checkedNames&quot;&gt;</div><div class="line">&lt;label for=&quot;mike&quot;&gt;Mike&lt;/label&gt;</div><div class="line">&lt;br&gt;</div><div class="line">&lt;span&gt;Checked names: &#123;&#123; checkedNames | json &#125;&#125;&lt;/span&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">new Vue(&#123;</div><div class="line">  el: &apos;...&apos;,</div><div class="line">  data: &#123;</div><div class="line">    checkedNames: []</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h4 id="Radio"><a href="#Radio" class="headerlink" title="Radio"></a>Radio</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;input type=&quot;radio&quot; id=&quot;one&quot; value=&quot;One&quot; v-model=&quot;picked&quot;&gt;</div><div class="line">&lt;label for=&quot;one&quot;&gt;One&lt;/label&gt;</div><div class="line">&lt;br&gt;</div><div class="line">&lt;input type=&quot;radio&quot; id=&quot;two&quot; value=&quot;Two&quot; v-model=&quot;picked&quot;&gt;</div><div class="line">&lt;label for=&quot;two&quot;&gt;Two&lt;/label&gt;</div><div class="line">&lt;br&gt;</div><div class="line">&lt;span&gt;Picked: &#123;&#123; picked &#125;&#125;&lt;/span&gt;</div></pre></td></tr></table></figure>
<h4 id="Select"><a href="#Select" class="headerlink" title="Select"></a>Select</h4><ul>
<li><p>单选，value为单个字符串</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;select v-model=&quot;selected&quot;&gt;</div><div class="line">	  &lt;option selected&gt;A&lt;/option&gt;</div><div class="line">	  &lt;option&gt;B&lt;/option&gt;</div><div class="line">	  &lt;option&gt;C&lt;/option&gt;</div><div class="line">&lt;/select&gt;</div><div class="line">&lt;span&gt;Selected: &#123;&#123; selected &#125;&#125;&lt;/span&gt;</div></pre></td></tr></table></figure>
</li>
<li><p>多选，value为一个数组</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"> &lt;select v-model=&quot;selected&quot; multiple&gt;</div><div class="line">	  &lt;option selected&gt;A&lt;/option&gt;</div><div class="line">	  &lt;option&gt;B&lt;/option&gt;</div><div class="line">	  &lt;option&gt;C&lt;/option&gt;</div><div class="line">&lt;/select&gt;</div><div class="line">&lt;br&gt;</div><div class="line">&lt;span&gt;Selected: &#123;&#123; selected | json &#125;&#125;&lt;/span&gt;</div></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="参数特性"><a href="#参数特性" class="headerlink" title="参数特性"></a>参数特性</h4><ul>
<li><p>lazy</p>
<p>在默认情况下，<code>v-model</code>是在<code>input</code>事件中同步输入框值和数据，可以添加一个<code>lazy</code>属性，从而在<code>change</code>事件中同步</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;!-- 在 &quot;change&quot; 而不是 &quot;input&quot; 事件中更新 --&gt;</div><div class="line">&lt;input v-model=&quot;msg&quot; lazy&gt;</div></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>debounce</p>
<p><code>debounce</code>设置一个最小延时，在每次敲击后延时同步输入框和数据，例如在输入过程中发送ajax请求时比较有用。<code>debounce</code>延迟写入底层数据，应当用<code>vm.$watch()</code>监听数据的变化。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;input v-model=&quot;msg&quot; debounce=&quot;500&quot;&gt;</div></pre></td></tr></table></figure>
<p><code>debounce</code>参数不会延迟DOM，若想延迟DOM事件，应当使用<code>debounce过滤器</code>。包装处理器，让它延迟执行，包装后的处理器将延迟<code>x</code>ms（默认为300ms）后执行，如果在延迟结束前再次调用，延时时长重置为<code>x</code>ms</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;input @keyup=&quot;onKeyup | debounce 500&quot;&gt;</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="方法和事件处理器"><a href="#方法和事件处理器" class="headerlink" title="方法和事件处理器"></a>方法和事件处理器</h3><h4 id="方法处理器"><a href="#方法处理器" class="headerlink" title="方法处理器"></a>方法处理器</h4><p>可以通过<code>v-on</code>指令监听DOM事件，事件监听程序都是写在<code>methods</code>属性中。在绑定方的时候，还可以传递参数，包括event对象，在HTML语句中需要使用<code>$event</code>进行传递。并且这个<code>$event</code>是原生的event对象，包含原生的event事件属性和方法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;div id=&quot;example&quot;&gt;</div><div class="line">  &lt;button v-on:click=&quot;greet($event, &apos;Vue.js&apos;)&quot;&gt;Greet&lt;/button&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<p>绑定一个事件处理方法<code>greet</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">var vm = new Vue(&#123;</div><div class="line">  el: &apos;#example&apos;,</div><div class="line">  // 在 `methods` 对象中定义方法</div><div class="line">  methods: &#123;</div><div class="line">    greet: function (event, msg) &#123;</div><div class="line">      // 方法内 `this` 指向 vm</div><div class="line">      alert(&apos;Hello &apos; + msg + &apos;!&apos;)</div><div class="line">      // `event` 是原生 DOM 事件</div><div class="line">      alert(event.target.tagName)</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">// 也可以在 JavaScript 代码中调用方法</div><div class="line">vm.greet() // -&gt; &apos;Hello Vue.js!&apos;</div></pre></td></tr></table></figure>
<h4 id="事件修饰符"><a href="#事件修饰符" class="headerlink" title="事件修饰符"></a>事件修饰符</h4><p>在事件处理器中，通常需要使用到<code>event.stopPropagation()</code>和<code>event.preventDefault()</code>，在Vue.js中为<code>v-on</code>指令添加了<code>.stop</code>和<code>.prevent</code>两个命令修饰符</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">&lt;!-- 阻止单击事件冒泡 --&gt;</div><div class="line">&lt;a v-on:click.stop=&quot;doThis&quot;&gt;&lt;/a&gt;</div><div class="line"></div><div class="line">&lt;!-- 提交事件不再重载页面 --&gt;</div><div class="line">&lt;form v-on:submit.prevent=&quot;onSubmit&quot;&gt;&lt;/form&gt;</div><div class="line"></div><div class="line">&lt;!-- 修饰符可以串联 --&gt;</div><div class="line">&lt;a v-on:click.stop.prevent=&quot;doThat&quot;&gt;</div><div class="line"></div><div class="line">&lt;!-- 只有修饰符 --&gt;</div><div class="line">&lt;form v-on:submit.prevent&gt;&lt;/form&gt;</div><div class="line"></div><div class="line">&lt;!-- 添加事件侦听器时使用 capture 模式 --&gt;</div><div class="line">&lt;div v-on:click.capture=&quot;doThis&quot;&gt;...&lt;/div&gt;</div><div class="line"></div><div class="line">&lt;!-- 只当事件在该元素本身（而不是子元素）触发时触发回调 --&gt;</div><div class="line">&lt;div v-on:click.self=&quot;doThat&quot;&gt;...&lt;/div&gt;</div></pre></td></tr></table></figure>
<h4 id="按键修饰符"><a href="#按键修饰符" class="headerlink" title="按键修饰符"></a>按键修饰符</h4><p>在监听键盘事件时，经常需要检测keyCode。Vue.js允许为<code>v-on</code>添加按键修饰符</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;!-- 同上 --&gt;</div><div class="line">&lt;input v-on:keyup.enter=&quot;submit&quot;&gt;</div><div class="line"></div><div class="line">&lt;!-- 缩写语法 --&gt;</div><div class="line">&lt;input @keyup.enter=&quot;submit&quot;&gt;</div></pre></td></tr></table></figure>
<p>全部按键的别名包括：</p>
<ul>
<li>enter</li>
<li>tab</li>
<li>delete</li>
<li>esc</li>
<li>space</li>
<li>up</li>
<li>down</li>
<li>left</li>
<li>right</li>
</ul>
<h3 id="组件"><a href="#组件" class="headerlink" title="组件"></a>组件</h3><p>组件（component）是Vue.js最强大的功能之一。组件可以扩展HTML元素，封装可重用的代码。组件的使用过程包括定义和注册的过程。组件在注册之后便可以在父实例中以自定义的元素的形式使用，不过要确保在初始化根实例之前注册了组件。而且自定义元素只是作为一个挂载点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;div id=&quot;example&quot;&gt;</div><div class="line">  &lt;my-component&gt;&lt;/my-component&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">// 定义</div><div class="line">var MyComponent = Vue.extend(&#123;</div><div class="line">  template: &apos;&lt;div&gt;A custom component!&lt;/div&gt;&apos;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">// 注册</div><div class="line">Vue.component(&apos;my-component&apos;, MyComponent)</div><div class="line"></div><div class="line">// 创建根实例</div><div class="line">new Vue(&#123;</div><div class="line">  el: &apos;#example&apos;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>渲染为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;div id=&quot;example&quot;&gt;</div><div class="line">  &lt;div&gt;A custom component!&lt;/div&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<h4 id="局部注册"><a href="#局部注册" class="headerlink" title="局部注册"></a>局部注册</h4><p>有点时候并不需要注册全局组件，可以只让组件用在其他组件内部，用<code>components</code>进行注册</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">//定义子组件</div><div class="line">var child = Vue.extend(&#123;</div><div class="line">    template: &apos;&lt;div&gt;this is child component &lt;/div&gt;&apos;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">//定义父组件</div><div class="line">var parent = Vue.extend(&#123;</div><div class="line">    template: &apos;&lt;div&gt;this is parent component &lt;child-component&gt;&lt;/child-component&gt; &lt;/div&gt;&apos;,</div><div class="line">    components: &#123;</div><div class="line">        //此时子组件child-component只能在父组件中使用</div><div class="line">        &apos;child-component&apos;: child</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">//注册父组件</div><div class="line">Vue.component(&apos;parent-component&apos;, parent);</div></pre></td></tr></table></figure>
<h4 id="模板解析"><a href="#模板解析" class="headerlink" title="模板解析"></a>模板解析</h4><p>Vue模板是DOM模板，使用浏览器原生的解析器而不是自己实现一个，所以HTML元素对什么元素可以放在它里面是有限制的。常见的限制如下：</p>
<ul>
<li>a 不能包含其它的交互元素（如按钮，链接）</li>
<li>ul 和 ol 只能直接包含 li</li>
<li>select 只能包含 option 和 optgroup</li>
<li>table 只能直接包含 thead, tbody, tfoot, tr, caption, col, colgroup</li>
<li>tr 只能直接包含 th 和 td</li>
</ul>
<h4 id="组件选项"><a href="#组件选项" class="headerlink" title="组件选项"></a>组件选项</h4><p>在组件内部也会使用数据变量，变量值也会存在data选项中，但是不同于Vue实例的data属性，组件内部的data属性应该是一个函数，在这个函数中返回一个新的对象，其中包含需要的属性名和属性值。</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">var MyComponent = Vue.extend(&#123;</div><div class="line">  data: function () &#123;</div><div class="line">    return &#123; a: 1 &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h4 id="使用props进行传值"><a href="#使用props进行传值" class="headerlink" title="使用props进行传值"></a>使用props进行传值</h4><p>组件实例的作用域是孤立的，在子组件中不能直接使用父组件中的属性。可以使用props属性将数据传递给子组件。是传递字面量还是动态值通过是否使用<code>v-bind</code>指令来确定。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">var child = Vue.extend(&#123;</div><div class="line">    template: &apos;&lt;div&gt;this is child component  &#123;&#123;attrA&#125;&#125; &#123;&#123;attrB&#125;&#125;&lt;/div&gt;&apos;,</div><div class="line">    props: [&apos;attrA&apos;, &apos;attrB&apos;]</div><div class="line">&#125;);</div><div class="line"></div><div class="line">var parent = Vue.extend(&#123;</div><div class="line">    template: &apos;&lt;div&gt;this is parent component &#123;&#123;a&#125;&#125; &apos; +</div><div class="line">              &apos;&lt;child-component v-bind:a=&quot;aa&quot; attr-a=&quot;attrA&quot; v-bind:attr-b=&quot;attrB&quot;&gt;&lt;/child-component&gt;&apos; +</div><div class="line">              &apos;&lt;/div&gt;&apos;,</div><div class="line">    components: &#123;</div><div class="line">        &apos;child-component&apos;: child</div><div class="line">    &#125;,</div><div class="line">    data: function () &#123;</div><div class="line">        return &#123;</div><div class="line">            attrA: dynamic  attrA&apos;,</div><div class="line">            attrB: &apos;dynamic attrB&apos;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>渲染为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div&gt;this is child component  attrA dynamic attrB&lt;/div&gt;</div></pre></td></tr></table></figure>
<p><strong>注意：子组件内部具有的属性不能和继承的属性相同，即不能在子组件的data和props中出现相同的变量名，否则程序会报错</strong></p>
<h4 id="父子组件通信"><a href="#父子组件通信" class="headerlink" title="父子组件通信"></a>父子组件通信</h4><p>子组件可以通过<code>this.$parent</code>访问它的父组件，父组件有一个<code>this.$children</code>，包含子组件的列表。根实例的后代通过<code>this.$root</code>访问它。</p>
<p>尽管可以访问到父链上的任意实例，不过子组件应该避免直接依赖父组件的数据，而是通过props属性进行传递，而且在子组件中也不要修改父组件中的状态，理想情况下只有组件自己才可以修改它的状态。</p>
<h4 id="自定义事件"><a href="#自定义事件" class="headerlink" title="自定义事件"></a>自定义事件</h4><p>Vue.js实现了一个自定义的事件接口，用于在组件树中通信。这个事件系统独立于原声DOM事件，用法也不同。</p>
<p>每个Vue实例都是一个事件触发器</p>
<ul>
<li>使用<code>$on</code>监听事件</li>
<li>使用<code>$emit</code>在它上面触发事件</li>
<li>使用<code>$dispatch</code>派发事件，事件沿着父链冒泡。<strong>不同于DOM原生事件，Vue事件在冒泡过程中第一次触发回调后自动停止冒泡，除非回调函数明确返回<code>true</code></strong></li>
<li>使用<code>$broadcast</code>广播事件，事件向下传导给所有后代</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&lt;!-- 子组件模板 --&gt;</div><div class="line">&lt;template id=&quot;child-template&quot;&gt;</div><div class="line">  &lt;input v-model=&quot;msg&quot;&gt;</div><div class="line">  &lt;button v-on:click=&quot;notify&quot;&gt;Dispatch Event&lt;/button&gt;</div><div class="line">&lt;/template&gt;</div><div class="line"></div><div class="line">&lt;!-- 父组件模板 --&gt;</div><div class="line">&lt;div id=&quot;events-example&quot;&gt;</div><div class="line">  &lt;p&gt;Messages: &#123;&#123; messages | json &#125;&#125;&lt;/p&gt;</div><div class="line">  &lt;child&gt;&lt;/child&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">// 注册子组件</div><div class="line">// 将当前消息派发出去</div><div class="line">Vue.component(&apos;child&apos;, &#123;</div><div class="line">  template: &apos;#child-template&apos;,</div><div class="line">  data: function () &#123;</div><div class="line">    return &#123; msg: &apos;hello&apos; &#125;</div><div class="line">  &#125;,</div><div class="line">  methods: &#123;</div><div class="line">    notify: function () &#123;</div><div class="line">      if (this.msg.trim()) &#123;</div><div class="line">        this.$dispatch(&apos;child-msg&apos;, this.msg)</div><div class="line">        this.msg = &apos;&apos;</div><div class="line">      &#125;</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">// 初始化父组件</div><div class="line">// 将收到消息时将事件推入一个数组</div><div class="line">var parent = new Vue(&#123;</div><div class="line">  el: &apos;#events-example&apos;,</div><div class="line">  data: &#123;</div><div class="line">    messages: []</div><div class="line">  &#125;,</div><div class="line">  // 在创建实例时 `events` 选项简单地调用 `$on`</div><div class="line">  events: &#123;</div><div class="line">    &apos;child-msg&apos;: function (msg) &#123;</div><div class="line">      // 事件回调内的 `this` 自动绑定到注册它的实例上</div><div class="line">      this.messages.push(msg);</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h4 id="使用slot分发内容"><a href="#使用slot分发内容" class="headerlink" title="使用slot分发内容"></a>使用slot分发内容</h4><p>每个组件都有自己的作用域，父组件模板的内容在父组件作用域内编译，子组件模板的内容在子组件作用域内编译。如果要绑定一个指令打到组件的根节点上，应该按如下方式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">Vue.component(&apos;child-component&apos;, &#123;</div><div class="line">  // v-show指令有效，因为是在正确的作用域内</div><div class="line">  template: &apos;&lt;div v-show=&quot;someChildProperty&quot;&gt;Child&lt;/div&gt;&apos;,</div><div class="line">  data: function () &#123;</div><div class="line">    return &#123;</div><div class="line">      someChildProperty: true</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>类似的，分发内容也是在父组件的作用域内编译。</p>
<ul>
<li><p>slot</p>
<p>父组件的内容在渲染的时候会被抛弃，除非子组件包含了<code>slot</code>标签。如果子组件只包含一个<code>slot</code>，则父组件的内容将被添加到<code>slot</code>标签中；如果子组件具有多个<code>slot</code>标签，可以通过<code>slot</code>标签的<code>name</code>属性进行分发。在具有多个<code>slot</code>标签时，仍然可以有一个匿名的<code>slot</code>，它是默认的<code>slot</code>，作为找不到匹配的内容的<code>slot</code>。</p>
<p>子组件模板：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"> &lt;div&gt;</div><div class="line">	  &lt;slot name=&quot;one&quot;&gt;&lt;/slot&gt;</div><div class="line">	  &lt;slot&gt;&lt;/slot&gt;</div><div class="line">	  &lt;slot name=&quot;two&quot;&gt;&lt;/slot&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<p>父组件模板：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;multi-insertion&gt;</div><div class="line"> &lt;p slot=&quot;one&quot;&gt;One&lt;/p&gt;</div><div class="line"> &lt;p slot=&quot;two&quot;&gt;Two&lt;/p&gt;</div><div class="line"> &lt;p&gt;Default A&lt;/p&gt;</div><div class="line"> &lt;/multi-insertion&gt;</div></pre></td></tr></table></figure>
<p>渲染为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;div&gt;</div><div class="line">&lt;p slot=&quot;one&quot;&gt;One&lt;/p&gt;</div><div class="line">&lt;p&gt;Default A&lt;/p&gt;</div><div class="line">&lt;p slot=&quot;two&quot;&gt;Two&lt;/p&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="编写可复用的组件"><a href="#编写可复用的组件" class="headerlink" title="编写可复用的组件"></a>编写可复用的组件</h4><p>在编写组件的时候，如果需要达到可复用的目的，应当定义一个清晰的公开接口。Vue.js组件的API来自三个部分：props、事件和slot</p>
<ul>
<li><strong>props</strong>允许外部环境传递数据给组件</li>
<li><strong>事件</strong>允许组件触发外部环境的action</li>
<li><strong>slot</strong>允许外部环境的内容插入到组件内部结构中</li>
</ul>
<p>通常一个组件模板的清晰结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"> &lt;my-component</div><div class="line">  :foo=&quot;baz&quot;</div><div class="line">  :bar=&quot;qux&quot;</div><div class="line">  @event-a=&quot;doThis&quot;</div><div class="line">  @event-b=&quot;doThat&quot;&gt;</div><div class="line">  &lt;!-- content --&gt;</div><div class="line">  &lt;img slot=&quot;icon&quot; src=&quot;...&quot;&gt;</div><div class="line">  &lt;p slot=&quot;main-text&quot;&gt;Hello!&lt;/p&gt;</div><div class="line">&lt;/my-component&gt;</div></pre></td></tr></table></figure>
<h3 id="自定义指令"><a href="#自定义指令" class="headerlink" title="自定义指令"></a>自定义指令</h3><p>除了Vue内置的指令外，还可以自定义指令。自定义指令提供一种机制将数据变化映射为DOM行为。通过<code>Vue.directive(id, definition)</code>方法注册一个全局自定义指令，它接收两个参数，指令ID和定义对象。在HTML元素中使用自定义指令时，需要添加<code>v-</code>前缀</p>
<h4 id="钩子函数"><a href="#钩子函数" class="headerlink" title="钩子函数"></a>钩子函数</h4><p>定义对象中定义了几个可选的钩子函数</p>
<ul>
<li>bind：只调用一次，在指令第一次绑定到元素上时调用</li>
<li>update：在bind后立即以初始值为参数第一次调用，以后每次绑定的值发生变化时，触发一次，参数为新值和旧值</li>
<li>unbind：只调用一次，在指令元素上解绑时调用</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">Vue.directive(&apos;my-directive&apos;, &#123;</div><div class="line">	  bind: function () &#123;</div><div class="line">	    // 准备工作</div><div class="line">	    // 例如，添加事件处理器或只需要运行一次的高耗任务</div><div class="line">	  &#125;,</div><div class="line">	  update: function (newValue, oldValue) &#123;</div><div class="line">	    // 值更新时的工作</div><div class="line">	    // 也会以初始值为参数调用一次</div><div class="line">	  &#125;,</div><div class="line">	  unbind: function () &#123;</div><div class="line">	    // 清理工作</div><div class="line">	    // 例如，删除 bind() 添加的事件监听器</div><div class="line">	  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h4 id="指令实例属性"><a href="#指令实例属性" class="headerlink" title="指令实例属性"></a>指令实例属性</h4><p>在钩子函数内<code>this</code>执行这个指令对象。这个对象暴露了一些实例属性：</p>
<ul>
<li><strong>el</strong>：指令绑定的元素</li>
<li><strong>vm</strong>：拥有该指令的上下文ViewModel</li>
<li><strong>expression</strong>：指令表达式</li>
<li><strong>arg</strong>：指令参数</li>
<li><strong>name</strong>：指令名称</li>
<li><strong>modifiers</strong>：指令修饰符对象</li>
<li><strong>descriptor</strong>：指令解析的结果对象</li>
</ul>
<p><strong>注意:这些属性我们应该将其视为可读的，不要修改他们。可以添加自定义属性，但不要覆盖已有的内部属性</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div id=&quot;demo&quot; v-demo:hello.a.b=&quot;msg&quot;&gt;&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"> Vue.directive(&apos;demo&apos;, &#123;</div><div class="line">	  bind: function () &#123;</div><div class="line">	    console.log(&apos;demo bound!&apos;)</div><div class="line">	  &#125;,</div><div class="line">	  update: function (value) &#123;</div><div class="line">	    this.el.innerHTML =</div><div class="line">	      &apos;name - &apos;       + this.name + &apos;&lt;br&gt;&apos; +</div><div class="line">	      &apos;expression - &apos; + this.expression + &apos;&lt;br&gt;&apos; +</div><div class="line">	      &apos;argument - &apos;   + this.arg + &apos;&lt;br&gt;&apos; +</div><div class="line">	      &apos;modifiers - &apos;  + JSON.stringify(this.modifiers) + &apos;&lt;br&gt;&apos; +</div><div class="line">	      &apos;value - &apos;      + value</div><div class="line">	  &#125;</div><div class="line">&#125;);</div><div class="line">var demo = new Vue(&#123;</div><div class="line">	  el: &apos;#demo&apos;,</div><div class="line">	  data: &#123;</div><div class="line">	    msg: &apos;hello!&apos;</div><div class="line">	  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>结果为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">name - demo</div><div class="line">expression - msg</div><div class="line">argument - hello</div><div class="line">modifiers - &#123;a: true, b: true&#125;</div><div class="line">value - hello!</div></pre></td></tr></table></figure>
<h4 id="高级选项-params"><a href="#高级选项-params" class="headerlink" title="高级选项-params"></a>高级选项-params</h4><p>类似于自定义组件的<code>props</code>属性，自定义指令提供<code>params</code>属性，指令一个数组列表，Vue编译器将自动提取绑定元素的这些特性。而且<code>params</code>还支持动态属性，另外还可以指定一个回调，在值变化时调用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;div v-example v-bind:a=&quot;someValue&quot;&gt;&lt;/div&gt;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">Vue.directive(&apos;example&apos;, &#123;</div><div class="line">  params: [&apos;a&apos;],</div><div class="line">  paramWatchers: &#123;</div><div class="line">    a: function (val, oldVal) &#123;</div><div class="line">      console.log(&apos;a changed!&apos;)</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<h3 id="自定义过滤器"><a href="#自定义过滤器" class="headerlink" title="自定义过滤器"></a>自定义过滤器</h3><p>类似于自定义指令，可以通过<code>Vue.filter()</code>注册一个全局自定义过滤器，接收两个参数：过滤器ID和过滤器函数，过滤器函数以值为参数，返回转换后的值</p>
<p>自定义过滤器可以接收任意数量的参数，第一个参数为表达式的值，从第二个参数开始可以传递任意值，字面量或者计算表达式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">Vue.filter(&apos;wrap&apos;, function (value, begin, end) &#123;</div><div class="line">    return begin + value + end</div><div class="line">&#125;);</div><div class="line"></div><div class="line">var vm = new Vue(&#123;</div><div class="line">    el: &apos;#demo&apos;,</div><div class="line">    data: &#123;</div><div class="line">   	   after: &apos;last&apos;</div><div class="line">    &#125;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;!-- &apos;hello&apos; =&gt; &apos;before hello after&apos; --&gt;</div><div class="line">&lt;span v-text=&quot;message | wrap &apos;before&apos; after&quot;&gt;&lt;/span&gt;</div></pre></td></tr></table></figure>
</span>
      
    </div>

    <footer class="post-footer">

      
        <div class="post-nav">
          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/28/felix/web-secure-skill/" rel="prev">Web安全技术</a>
            
          </div>

          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/08/y8n/vue-in-action-vue-project/" rel="next">Vue实战（二）—— 在项目中的实践</a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>

 </div>

        

        
          <div class="comments" id="comments">
            <div id="SOHUCS" sid="" ></div>
          </div>
        
      </div>

      
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <a href="https://github.com/xgfe" target="_blank"><img class="site-author-image" src="http://p0.meituan.net/xgfe/082a9624ba5ae8602150a2d43968463e49348.png" alt="xgfe" itemprop="image"/></a>
          <p class="site-author-name" itemprop="name">xgfe</p>
        </div>
        <p class="site-description motion-element" itemprop="description">xgfe's blog. 鲜果前端的技术博客，鲜果前端研发部官方博客。前端基础技术研究：html, html5, javascript, css, css3；前端框架研究：angularJs, react, react native.</p>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">89</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          <div class="site-state-item site-state-categories">
            
              <span class="site-state-item-count">37</span>
              <span class="site-state-item-name">作者</span>
              
          </div>

          <div class="site-state-item site-state-tags">
            <a href="/tags">
              <span class="site-state-item-count">131</span>
              <span class="site-state-item-name">标签</span>
              </a>
          </div>

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" target="_blank" rel="alternate">
              <i class="menu-item-icon icon-next-feed"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/xgfe" target="_blank">GitHub</a>
              </span>
            
          
        </div>

        
        

        <div class="links-of-author motion-element">
          
        </div>

      </section>

      
        <section class="post-toc-wrap sidebar-panel-active">
          <div class="post-toc-indicator-top post-toc-indicator"></div>
          <div class="post-toc">
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Vue基础"><span class="nav-number">1.</span> <span class="nav-text">Vue基础</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Vue实例"><span class="nav-number">1.1.</span> <span class="nav-text">Vue实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#构造器"><span class="nav-number">1.1.1.</span> <span class="nav-text">构造器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例生命周期"><span class="nav-number">1.1.2.</span> <span class="nav-text">实例生命周期</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据绑定"><span class="nav-number">1.2.</span> <span class="nav-text">数据绑定</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#插值"><span class="nav-number">1.2.1.</span> <span class="nav-text">插值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#绑定表达式"><span class="nav-number">1.2.2.</span> <span class="nav-text">绑定表达式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#过滤器"><span class="nav-number">1.2.3.</span> <span class="nav-text">过滤器</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#指令"><span class="nav-number">1.3.</span> <span class="nav-text">指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#计算属性"><span class="nav-number">1.4.</span> <span class="nav-text">计算属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Class与style绑定"><span class="nav-number">1.5.</span> <span class="nav-text">Class与style绑定</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#绑定class"><span class="nav-number">1.5.1.</span> <span class="nav-text">绑定class</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#绑定style内联样式"><span class="nav-number">1.5.2.</span> <span class="nav-text">绑定style内联样式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#表单控件绑定"><span class="nav-number">1.6.</span> <span class="nav-text">表单控件绑定</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Text"><span class="nav-number">1.6.1.</span> <span class="nav-text">Text</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Checkbox"><span class="nav-number">1.6.2.</span> <span class="nav-text">Checkbox</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Radio"><span class="nav-number">1.6.3.</span> <span class="nav-text">Radio</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Select"><span class="nav-number">1.6.4.</span> <span class="nav-text">Select</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数特性"><span class="nav-number">1.6.5.</span> <span class="nav-text">参数特性</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#方法和事件处理器"><span class="nav-number">1.7.</span> <span class="nav-text">方法和事件处理器</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#方法处理器"><span class="nav-number">1.7.1.</span> <span class="nav-text">方法处理器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#事件修饰符"><span class="nav-number">1.7.2.</span> <span class="nav-text">事件修饰符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#按键修饰符"><span class="nav-number">1.7.3.</span> <span class="nav-text">按键修饰符</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#组件"><span class="nav-number">1.8.</span> <span class="nav-text">组件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#局部注册"><span class="nav-number">1.8.1.</span> <span class="nav-text">局部注册</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#模板解析"><span class="nav-number">1.8.2.</span> <span class="nav-text">模板解析</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#组件选项"><span class="nav-number">1.8.3.</span> <span class="nav-text">组件选项</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用props进行传值"><span class="nav-number">1.8.4.</span> <span class="nav-text">使用props进行传值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#父子组件通信"><span class="nav-number">1.8.5.</span> <span class="nav-text">父子组件通信</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#自定义事件"><span class="nav-number">1.8.6.</span> <span class="nav-text">自定义事件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用slot分发内容"><span class="nav-number">1.8.7.</span> <span class="nav-text">使用slot分发内容</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#编写可复用的组件"><span class="nav-number">1.8.8.</span> <span class="nav-text">编写可复用的组件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义指令"><span class="nav-number">1.9.</span> <span class="nav-text">自定义指令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#钩子函数"><span class="nav-number">1.9.1.</span> <span class="nav-text">钩子函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#指令实例属性"><span class="nav-number">1.9.2.</span> <span class="nav-text">指令实例属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#高级选项-params"><span class="nav-number">1.9.3.</span> <span class="nav-text">高级选项-params</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义过滤器"><span class="nav-number">1.10.</span> <span class="nav-text">自定义过滤器</span></a></li></ol></li></ol></div>
            
          </div>
          <div class="post-toc-indicator-bottom post-toc-indicator"></div>
        </section>
      

    </div>
  </aside>


    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner"> <div class="copyright" >
  
  &copy; &nbsp; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="icon-next-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xgfe</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" target="_blank" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


 </div>
    </footer>

    <div class="back-to-top"></div>
  </div>

  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  
  
    <script type="text/javascript"> 
(function(){ 
var appid = 'cysWiXvkm'; 
var conf = 'prod_fc970dbe85103c7a79b2c4f3dc7fb190'; 
var width = window.innerWidth || document.documentElement.clientWidth; 
if (width < 960) { 
window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="http://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("http://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})}); } })(); </script>
    

  


  
  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js"></script>
  <script type="text/javascript" src="/js/fancy-box.js?v=0.4.5.1"></script>


  <script type="text/javascript" src="/js/helpers.js?v=0.4.5.1"></script>
  

  <script type="text/javascript" src="/vendors/velocity/velocity.min.js"></script>
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js"></script>

  <script type="text/javascript" src="/js/motion_global.js?v=0.4.5.1" id="motion.global"></script>




  <script type="text/javascript" src="/js/nav-toggle.js?v=0.4.5.1"></script>
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  
<script type="text/javascript" src="/js/bootstrap.scrollspy.js?v=0.4.5.1" id="bootstrap.scrollspy.custom"></script>


<script type="text/javascript" id="sidebar.toc.highlight">
  $(document).ready(function () {
    var tocSelector = '.post-toc';
    var $tocSelector = $(tocSelector);
    var activeCurrentSelector = '.active-current';

    $tocSelector
      .on('activate.bs.scrollspy', function () {
        var $currentActiveElement = $(tocSelector + ' .active').last();

        removeCurrentActiveClass();
        $currentActiveElement.addClass('active-current');

        $tocSelector[0].scrollTop = $currentActiveElement.position().top;
      })
      .on('clear.bs.scrollspy', function () {
        removeCurrentActiveClass();
      });

    function removeCurrentActiveClass () {
      $(tocSelector + ' ' + activeCurrentSelector)
        .removeClass(activeCurrentSelector.substring(1));
    }

    function processTOC () {
      getTOCMaxHeight();
      toggleTOCOverflowIndicators();
    }

    function getTOCMaxHeight () {
      var height = $('.sidebar').height() -
                   $tocSelector.position().top -
                   $('.post-toc-indicator-bottom').height();

      $tocSelector.css('height', height);

      return height;
    }

    function toggleTOCOverflowIndicators () {
      tocOverflowIndicator(
        '.post-toc-indicator-top',
        $tocSelector.scrollTop() > 0 ? 'show' : 'hide'
      );

      tocOverflowIndicator(
        '.post-toc-indicator-bottom',
        $tocSelector.scrollTop() >= $tocSelector.find('ol').height() - $tocSelector.height() ? 'hide' : 'show'
      )
    }

    $(document).on('sidebar.motion.complete', function () {
      processTOC();
    });

    $('body').scrollspy({ target: tocSelector });
    $(window).on('resize', function () {
      if ( $('.sidebar').hasClass('sidebar-active') ) {
        processTOC();
      }
    });

    onScroll($tocSelector);

    function onScroll (element) {
      element.on('mousewheel DOMMouseScroll', function (event) {
          var oe = event.originalEvent;
          var delta = oe.wheelDelta || -oe.detail;

          this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
          event.preventDefault();

          toggleTOCOverflowIndicators();
      });
    }

    function tocOverflowIndicator (indicator, action) {
      var $indicator = $(indicator);
      var opacity = action === 'show' ? 0.4 : 0;
      $indicator.velocity ?
        $indicator.velocity('stop').velocity({
          opacity: opacity
        }, { duration: 100 }) :
        $indicator.stop().animate({
          opacity: opacity
        }, 100);
    }

  });
</script>

<script type="text/javascript" id="sidebar.nav">
  $(document).ready(function () {
    var html = $('html');
    var TAB_ANIMATE_DURATION = 200;
    var hasVelocity = $.isFunction(html.velocity);

    $('.sidebar-nav li').on('click', function () {
      var item = $(this);
      var activeTabClassName = 'sidebar-nav-active';
      var activePanelClassName = 'sidebar-panel-active';
      if (item.hasClass(activeTabClassName)) {
        return;
      }

      var currentTarget = $('.' + activePanelClassName);
      var target = $('.' + item.data('target'));

      hasVelocity ?
        currentTarget.velocity('transition.slideUpOut', TAB_ANIMATE_DURATION, function () {
          target
            .velocity('stop')
            .velocity('transition.slideDownIn', TAB_ANIMATE_DURATION)
            .addClass(activePanelClassName);
        }) :
        currentTarget.animate({ opacity: 0 }, TAB_ANIMATE_DURATION, function () {
          currentTarget.hide();
          target
            .stop()
            .css({'opacity': 0, 'display': 'block'})
            .animate({ opacity: 1 }, TAB_ANIMATE_DURATION, function () {
              currentTarget.removeClass(activePanelClassName);
              target.addClass(activePanelClassName);
            });
        });

      item.siblings().removeClass(activeTabClassName);
      item.addClass(activeTabClassName);
    });

    $('.post-toc a').on('click', function (e) {
      e.preventDefault();
      var targetSelector = escapeSelector(this.getAttribute('href'));
      var offset = $(targetSelector).offset().top;
      hasVelocity ?
        html.velocity('stop').velocity('scroll', {
          offset: offset  + 'px',
          mobileHA: false
        }) :
        $('html, body').stop().animate({
          scrollTop: offset
        }, 500);
    });

    // Expand sidebar on post detail page by default, when post has a toc.
    var $tocContent = $('.post-toc-content');
    if (isDesktop() && CONFIG.sidebar === 'post') {
      if ($tocContent.length > 0 && $tocContent.html().trim().length > 0) {
        displaySidebar();
      }
    }
  });
</script>



  <script type="text/javascript">
    $(document).ready(function () {
      if (CONFIG.sidebar === 'always') {
        displaySidebar();
      }
      if (isMobile()) {
        FastClick.attach(document.body);
      }
    });
  </script>

  

  
  

  
  <script type="text/javascript" src="/js/lazyload.js"></script>
  <script type="text/javascript">
    $(function () {
      $("#posts").find('img').lazyload({
        placeholder: "/images/loading.gif",
        effect: "fadeIn"
      });
    });
  </script>

  <!-- google search, added by felix -->
  <script>
      $('#gg-form').on('submit', function(e) {
        var keyword = $.trim($(this).find('#gg-search-input').val());
        if (keyword) {
          location.href = 'https://www.google.com.hk/?gfe_rd=cr&ei=hXw8VpjtHuLC8AeSuIjQAg&gws_rd=ssl#safe=strict&q='+encodeURIComponent(keyword)+'+site:xgfe.github.io';
        }
        return false;
      });
  </script>
  <!-- baidu 站长自动推送 -->
  <script>
  (function(){
      var bp = document.createElement('script');
      bp.src = '//push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>
</body>
</html>
